/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package managedBean.TX.Rep;

import entity.establishment.OutletFood;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import org.primefaces.event.map.MarkerDragEvent;
import org.primefaces.model.map.Marker;
import session.TX.OutletSBRemote;

/**
 *
 * @author Randall Chia
 */
@ManagedBean(name = "createOutletMB")
@ViewScoped
public class CreateOutletMB {
    
    @EJB
    private OutletSBRemote outletSB;
    @ManagedProperty(value = "#{editOutletMB}")
    private EditOutletMB editOutletMB;

    /** Creates a new instance of CreateOutletMB */
    public CreateOutletMB() {
    }
    //Code for creating new OutletFood
    //----------------------------------------------------------------------------------------------------------------------
    //Data fields to input when making a new Generic Outlet. Specific fields are set by outlet managers
    private double latitude;
    private double longitude;
    private String outletName;
    private String blkNo;
    private String streetName;
    private String unitName;
    private String postalCode;
    private String phoneNo;
    private String websiteURL;
    private String email;
    private String priceRange;
    private String image;
    private String buildingName;
    private String operatingHrs;
    private Long outletId;
    private OutletFood selectedOutlet;
    
    public void makeNewOutlet(ActionEvent event) {

        //Check the lat and long
        if (getLongitude() < 103.593407 || getLongitude() > 104.045219 || getLatitude() < 1.235865 || getLatitude() > 1.473035) {
            System.out.println("Outside boundary!");
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Please ensure the location marker is inside Singapore!", ""));
            return;
        }

        //Pull the StakeholderID from Session
        Long stakeholderID = (Long) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("stakeholderId");

        //Pull the accountID from Session, this will be set as the outlet creator
        Long socialID = (Long) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("accountId");

        //Make the outlet
        String statusMSG = outletSB.addNewOutlet(
                stakeholderID,
                socialID,
                getLatitude(),
                getLongitude(),
                getOutletName(),
                getBlkNo(),
                getStreetName(),
                getUnitName(),
                getPostalCode(),
                getPhoneNo(),
                getWebsiteURL(),
                getEmail(),
                getPriceRange(),
                getImage(),
                getBuildingName(),
                getOperatingHrs());
        
        FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, statusMSG, ""));
        return;
    }
    
    public void editOutlet(ActionEvent event) {
        System.out.println("CreateOutletMB: editing outlet");
        System.out.println(outletId);
        if (outletId == null) {
            return;
        }
        //Check the lat and long
        if (getLongitude() < 103.593407 || getLongitude() > 104.045219 || getLatitude() < 1.235865 || getLatitude() > 1.473035) {
            System.out.println("Outside boundary!");
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Please ensure the location marker is inside Singapore!", ""));
            return;
        }

        //Make the outlet
        String statusMSG = outletSB.editOutlet(
                outletId,
                getLatitude(),
                getLongitude(),
                getOutletName(),
                getBlkNo(),
                getStreetName(),
                getUnitName(),
                getPostalCode(),
                getPhoneNo(),
                getWebsiteURL(),
                getEmail(),
                getPriceRange(),
                getImage(),
                getBuildingName(),
                getOperatingHrs());
        
        FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, statusMSG, ""));
        return;
    }
    
    public void updateOutletId(ActionEvent event) {
        this.outletId = (Long) event.getComponent().getAttributes().get("outletId");
    }
    
    public void getLatLong(MarkerDragEvent event) {
        Marker marker = (Marker) event.getMarker();
        
        latitude = marker.getLatlng().getLat();
        longitude = marker.getLatlng().getLng();
        
    }

    //----------------------------------------------------------------------------------------------------------------------
    //Get and Set
    /**
     * @return the outletSB
     */
    public OutletSBRemote getOutletSB() {
        return outletSB;
    }

    /**
     * @param outletSB the outletSB to set
     */
    public void setOutletSB(OutletSBRemote outletSB) {
        this.outletSB = outletSB;
    }

    /**
     * @return the latitude
     */
    public double getLatitude() {
        return latitude;
    }

    /**
     * @param latitude the latitude to set
     */
    public void setLatitude(double latitude) {
        this.latitude = latitude;
    }

    /**
     * @return the longitude
     */
    public double getLongitude() {
        return longitude;
    }

    /**
     * @param longitude the longitude to set
     */
    public void setLongitude(double longitude) {
        this.longitude = longitude;
    }

    /**
     * @return the outletName
     */
    public String getOutletName() {
        return outletName;
    }

    /**
     * @param outletName the outletName to set
     */
    public void setOutletName(String outletName) {
        this.outletName = outletName;
    }

    /**
     * @return the blkNo
     */
    public String getBlkNo() {
        return blkNo;
    }

    /**
     * @param blkNo the blkNo to set
     */
    public void setBlkNo(String blkNo) {
        this.blkNo = blkNo;
    }

    /**
     * @return the streetName
     */
    public String getStreetName() {
        return streetName;
    }

    /**
     * @param streetName the streetName to set
     */
    public void setStreetName(String streetName) {
        this.streetName = streetName;
    }

    /**
     * @return the unitName
     */
    public String getUnitName() {
        return unitName;
    }

    /**
     * @param unitName the unitName to set
     */
    public void setUnitName(String unitName) {
        this.unitName = unitName;
    }

    /**
     * @return the postalCode
     */
    public String getPostalCode() {
        return postalCode;
    }

    /**
     * @param postalCode the postalCode to set
     */
    public void setPostalCode(String postalCode) {
        this.postalCode = postalCode;
    }

    /**
     * @return the phoneNo
     */
    public String getPhoneNo() {
        return phoneNo;
    }

    /**
     * @param phoneNo the phoneNo to set
     */
    public void setPhoneNo(String phoneNo) {
        this.phoneNo = phoneNo;
    }

    /**
     * @return the websiteURL
     */
    public String getWebsiteURL() {
        return websiteURL;
    }

    /**
     * @param websiteURL the websiteURL to set
     */
    public void setWebsiteURL(String websiteURL) {
        this.websiteURL = websiteURL;
    }

    /**
     * @return the email
     */
    public String getEmail() {
        return email;
    }

    /**
     * @param email the email to set
     */
    public void setEmail(String email) {
        this.email = email;
    }

    /**
     * @return the priceRange
     */
    public String getPriceRange() {
        return priceRange;
    }

    /**
     * @param priceRange the priceRange to set
     */
    public void setPriceRange(String priceRange) {
        this.priceRange = priceRange;
    }

    /**
     * @return the image
     */
    public String getImage() {
        return image;
    }

    /**
     * @param image the image to set
     */
    public void setImage(String image) {
        this.image = image;
    }

    /**
     * @return the buildingName
     */
    public String getBuildingName() {
        return buildingName;
    }

    /**
     * @param buildingName the buildingName to set
     */
    public void setBuildingName(String buildingName) {
        this.buildingName = buildingName;
    }

    /**
     * @return the operatingHrs
     */
    public String getOperatingHrs() {
        return operatingHrs;
    }

    /**
     * @param operatingHrs the operatingHrs to set
     */
    public void setOperatingHrs(String operatingHrs) {
        this.operatingHrs = operatingHrs;
    }
    
    public Long getOutletId() {
        return outletId;
    }
    
    public void setOutletId(Long outletId) {
        this.outletId = outletId;
    }
    
    public OutletFood getSelectedOutlet() {
        return selectedOutlet;
    }
    
    public void setSelectedOutlet(OutletFood selectedOutlet) {
        this.selectedOutlet = selectedOutlet;
    }
    
    public void updateSelectedOutlet(OutletFood o) {
        System.out.println("creatoutletmb: updating selected outlet");
        this.outletId = o.getId();
        this.selectedOutlet = o;
        this.blkNo = o.getBlkNo();
        this.buildingName = o.getBuildingName();
        this.email = o.getEmail();
        this.latitude = o.getLatitude();
        this.longitude = o.getLongitude();
        this.operatingHrs = o.getOperatingHrs();
        this.phoneNo = o.getPhoneNo();
        this.postalCode = o.getPostalCode();
        this.priceRange = o.getPriceRange();
        this.streetName = o.getStreetName();
        this.unitName = o.getUnitName();
        this.websiteURL = o.getWebsiteURL();
        this.outletName = o.getOutletName();
        this.image=o.getImage();
    }

    public EditOutletMB getEditOutletMB() {
        return editOutletMB;
    }

    public void setEditOutletMB(EditOutletMB editOutletMB) {
        this.editOutletMB = editOutletMB;
    }
    
    @PostConstruct
    public void initializeEditOutletMB2() {
        editOutletMB.setOption(0);
    }
    
    @PreDestroy
    public void initializeEditOutletMB() {
        editOutletMB.setOption(0);
    }
}
